<?php

namespace common\models;

use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "{{%vote_player}}".
 *
 * @property integer $id
 * @property integer $uid
 * @property integer $cid
 * @property string $title
 * @property string $digest
 * @property string $thumb
 * @property integer $votes
 * @property integer $created_at
 * @property integer $updated_at
 */
class VotePlayer extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%vote_player}}';
    }

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['uid', 'cid', 'vid', 'title', 'digest'], 'required'],
            [['uid', 'cid', 'vid', 'votes'], 'integer'],
            [['digest'], 'string'],
            [['sort', 'votes'], 'default', 'value'=>0],
            [['title'], 'string', 'max' => 80],
            [['thumb'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg, gif', 'checkExtensionByMimeType' => false],
        ];
    }

    public function getCategory()
    {
        return $this->hasOne(Category::className(), ['id' => 'cid']);
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'uid' => '公众号ID',
            'cid' => '分类',
            'vid' => '活动',
            'title' => '标题',
            'digest' => '摘要',
            'thumb' => '缩略图',
            'votes' => '投票数',
            'sort' => '排序',
            'created_at' => '添加时间',
            'updated_at' => '更新时间',
        ];
    }
}
